


import os
import requests
import json

''' App Store Connect API '''
class AppStoreConnectAPI:
    
    def __init__(self, api_key, api_issuer_id):
        self.api_key = api_key
        self.api_issuer_id = api_issuer_id

    def _get_headers(self):
        return {
            'Authorization': f'Bearer {self.api_key}',
            'Content-Type': 'application/json'
        }

    def get_app_info(self, app_id):
        url = f"https://api.appstoreconnect.apple.com/v1/apps/{app_id}"
        response = requests.get(url, headers=self._get_headers())
        return response.json()

    def get_all_apps(self):
        url = "https://api.appstoreconnect.apple.com/v1/apps"
        response = requests.get(url, headers=self._get_headers())
        return response.json()
    
    ''' Get vendors '''
    def get_vendors(self):
        url = "https://api.appstoreconnect.apple.com/v1/vendors"
        response = requests.get(url, headers=self._get_headers())
        return response.json()
    
    def get_sales_report(self, vendor_id, report_date):
        url = f"https://api.appstoreconnect.apple.com/v1/salesReports?filter[vendorNumber]={vendor_id}&filter[reportDate]={report_date}&filter[reportType]=SALES&filter[reportSubType]=SUMMARY"
        response = requests.get(url, headers=self._get_headers())
        return response.json()

    def get_financial_report(self, vendor_id, fiscal_year, fiscal_period):
        url = f"https://api.appstoreconnect.apple.com/v1/financeReports?filter[vendorNumber]={vendor_id}&filter[fiscalYear]={fiscal_year}&filter[fiscalPeriod]={fiscal_period}"
        response = requests.get(url, headers=self._get_headers())
        return response.json()

